import { useEarthGroup } from "./EarthViewerGroup";
import * as Cesium from 'cesium'
import { FogEffect, SnowEffect, RainEffect } from '../Material/WeatherEffect.js'

export class useDrawWeather {
    // 图层组
    group: useEarthGroup;
    viewer: Cesium.Viewer;
    //绘制天气效果的对象
    WeatherObj: FogEffect | SnowEffect | RainEffect;

    constructor(group: useEarthGroup, view: Cesium.Viewer) {
        if (!group) {
            throw new Error("请将图层组传入--useDrawWeather")
        }

        this.group = group;
        this.viewer = view;

        return this;
    }
    /**
     * 绘制下雪天气
     */
    drawSnow = (type: string, item: any) => {

        if (this.WeatherObj) {
            this.WeatherObj.destroy();
            this.WeatherObj = null;
        }

        let flagType = this.viewer.scene.globe.weather;
        if (!flagType || flagType != type) {
            // 下雪效果
            this.WeatherObj = new SnowEffect(this.viewer, {
                snowSize: item?.snowSize || 0.02, // 雪花大小
                snowSpeed: item?.snowSpeed || 60.0, // 雪速
            })

        }

        this.WeatherObj ? this.viewer.scene.globe.weather = type : this.viewer.scene.globe.weather = null;
        return null;
    }
    /**
     * 绘制下雨天气
     */
    drawRain = (type: string, item: any) => {

        if (this.WeatherObj) {
            this.WeatherObj.destroy();
            this.WeatherObj = null;
        }

        let flagType = this.viewer.scene.globe.weather;

        if (!flagType || flagType != type) {
            // 下雪效果
            this.WeatherObj = new RainEffect(this.viewer, {
                tiltAngle: item?.tiltAngle || -0.6, //倾斜角度
                rainSize: item?.rainSize || 0.6, // 雨大小
                rainSpeed: item?.rainSpeed || 120.0 // 雨速
            })
        }

        this.WeatherObj ? this.viewer.scene.globe.weather = type : this.viewer.scene.globe.weather = null;
        return null;
    }
    /**
     * 绘制雾天气
     */
    drawFog = (type: string, item: any) => {

        if (this.WeatherObj) {
            this.WeatherObj.destroy();
            this.WeatherObj = null;
        }

        let flagType = this.viewer.scene.globe.weather;
        if (!flagType || flagType != type) {
            // 雾效果
            this.WeatherObj = new FogEffect(this.viewer, {
                visibility: item?.visibility || 0.2,
                color: item?.color ? Cesium.Color.fromCssColorString(item?.color) : new Cesium.Color(0.8, 0.8, 0.8, 0.3)
            })
        }

        this.WeatherObj ? this.viewer.scene.globe.weather = type : this.viewer.scene.globe.weather = null;
        return null;
    }
}